var express = require('express'),
  cookieParser = require('cookie-parser'),
  router = express.Router(),
  request = require('request'),
  crypto = require('crypto'),
  // cookie = require('./page/cookie');
  home = require('./page/home'),
  list = require('./page/list'),
  products = require('./page/products'),
  edit = require('./page/edit');
// pay = require('./page/pay'),
// orderList = require('./page/order-list'),
// diyRouter = require('./page/diy');

module.exports = function(app) {
  // 设置 Cookie[1:简体中文,2:繁体中文,3:英文]
  // 初始化中文
  // app.use(cookieParser());
  // app.use('/cookie', cookie);
  app.use('/', home);
  app.use('/list', list);
  app.use('/products', products);
  app.use('/edit', edit);
  // app.use('/pay', pay);
  // app.use('/order', orderList);
  // app.use('/diy', diyRouter);

  //反向代理（这里把需要进行反代的路径配置到这里即可）
  //eg:将/api/test 代理到 ${HOST}/api/test
  app.use('/ravimg', function(req, res) {
    const method = req.method.toLowerCase();
    const sreq = request[method](config.resourcePath.apiRoot + req.query.src);
    if (method === 'post' || method === 'put') {
      sreq.set('Content-Type', 'application/json').send(req.body)
    }
    sreq.pipe(res);
    sreq.on('end', function(error, res) {
      if (error) {
        console.log(error);
        return;
      }
    });
  });

  // app.get('/revMd5', function(req, res) {
  //   console.log(req.query);
  //   var timestamp = parseInt(parseInt(new Date().getTime()) / 1000),
  //     paySign = "appId=" + config.appid + "&nonceStr=" + config.getticket.noncestr + "&package=" + req.query.package + "&signType=MD5&timeStamp=" + timestamp + "&key=" + config.shopKey;
  //   paySign = crypto.createHash('md5').update(paySign, 'utf8').digest('hex');
  //   res.send({
  //     appId: config.appid,
  //     nonceStr: config.getticket.noncestr,
  //     timeStamp: timestamp.toString(),
  //     paySign: paySign,
  //     signType: "MD5",
  //     package: req.query.package
  //   });
  // });
};